//
//  LYQEditPositionPickerView.m
//  ZCProject
//
//  Created by Zheng on 2016/12/28.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQEditPositionPickerView.h"

#import "LYQEditPositionModel.h"

@implementation LYQEditPositionPickerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
    
        self.backgroundColor = [UIColor grayColor];
        self.hidden = YES;
        self.alpha = 0;
        
        [self addSubview:self.chooseView];
    }
    return self;
}

#pragma mark - lazy load subview
- (UIView *)chooseView {
    if (_chooseView) {
        return _chooseView;
    }
    
    _chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, kMS_Height-kWScale(250), kMS_Width, kMS_Height)];
    _chooseView.backgroundColor = RGB(240, 241, 245, 1);

    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kWScale(10), kMS_Width, kWScale(220))];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_chooseView addSubview:_pickerView];
    
    _cnlBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _cnlBtn.frame = CGRectMake(0, kWScale(5), kWScale(60), kWScale(30));
    [_cnlBtn setTitle:@"取消" forState:UIControlStateNormal];
    [_cnlBtn setTitleColor:kLYQBlue forState:UIControlStateNormal];
    _cnlBtn.titleLabel.font = kWFont(16);
    [_cnlBtn addTarget:self action:@selector(chooseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseView addSubview:_cnlBtn];
    
    _esrBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _esrBtn.frame = CGRectMake(kMS_Width-kWScale(60), kWScale(5), kWScale(60), kWScale(30));
    [_esrBtn setTitle:@"确定" forState:UIControlStateNormal];
    [_esrBtn setTitleColor:kLYQBlue forState:UIControlStateNormal];
    _esrBtn.titleLabel.font = kWFont(16);
    [_esrBtn addTarget:self action:@selector(chooseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseView addSubview:_esrBtn];
    
    return _chooseView;
}

#pragma mark - button action
- (void)chooseBtnAct:(UIButton *)sender {
    
    [self showStateActionWithStyle:_style];
    
    if (sender == _esrBtn) {
#pragma mark 确定
        
        if (_education) {
            _model.education = _education;
        } else {
            _education = @"不限";
            _model.education = _education;
        }
        
        if (_experience) {
            _model.experience = _experience;
        } else {
            _experience = @"不限";
            _model.experience = _experience;
        }
        
        _model.salary = [NSString stringWithFormat:@"%@k-%@K",_minSalary,_maxSalary];
        
        //更改数据修改UI
        [_delegate updateUIWithStyle:_style];
    }
}

#pragma mark - 显示选择器并设置数据
- (void)showStateActionWithStyle:(LYQEditPositionPickerViewStyle)style {
    
    _style = style;
    
    if (style == LYQEditPositionPickerViewStyleEducation) {
        _titlesArr = @[@"不限",@"大专",@"本科",@"硕士",@"博士"];
        [_pickerView selectRow:0 inComponent:0 animated:YES];

    } else if (style == LYQEditPositionPickerViewStyleExperience) {
        _titlesArr = @[@"不限",@"应届生",@"1年以内",@"1-3年",@"3-5年",@"5-10年",@"10年以上"];
        [_pickerView selectRow:0 inComponent:0 animated:YES];

    } else if (style == LYQEditPositionPickerViewStyleSalary) {
        
        _minArr = [NSMutableArray array];
        for (int i = 1; i < 50; i++) {
            [_minArr addObject:[NSString stringWithFormat:@"%d",i]];
        }
        
        _maxArr = [NSMutableArray array];
        for (int i = 2; i < 51; i++) {
            [_maxArr addObject:[NSString stringWithFormat:@"%d",i]];
        }
        
        _salaryArr = @[_minArr,_maxArr];
        
        if (!_minSalary) {
            _minSalary = @"1";
        }
        
        if (!_maxSalary) {
            _maxSalary = @"2";
        }
        
    }
    
    [_pickerView reloadAllComponents];
    
    if (style == LYQEditPositionPickerViewStyleSalary) {
        [_pickerView selectRow:0 inComponent:0 animated:YES];
        [_pickerView selectRow:0 inComponent:1 animated:YES];
    }
    
    
    if (self.hidden) {
        
        self.hidden = !self.hidden;
        
        _chooseView.hidden = NO;
        
        [UIView animateWithDuration:0.35 animations:^{
            self.alpha = 0.7;
            _chooseView.frame = CGRectMake(0, kMS_Height-kWScale(250), kMS_Width, kWScale(250));
        }];
        
        
    } else {
        
        [UIView animateWithDuration:0.35 animations:^{
            self.alpha = 0;
            _chooseView.frame = CGRectMake(0, kMS_Height, kMS_Width, kWScale(250));
        } completion:^(BOOL finished) {
            self.hidden = !self.hidden;
            _chooseView.hidden = YES;
            _minSalary = nil;
            _maxSalary = nil;
            _education = nil;
            _experience = nil;            
        }];
    }
}

#pragma mark - pickerview datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    if (_style == LYQEditPositionPickerViewStyleEducation || _style == LYQEditPositionPickerViewStyleExperience) {
        return 1;
    } else {
        return 2;
    }
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (_style == LYQEditPositionPickerViewStyleEducation || _style == LYQEditPositionPickerViewStyleExperience) {
        return _titlesArr.count;
    } else {

        return [_salaryArr[component] count];
    }
}

#pragma mark - pickerview delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (_style == LYQEditPositionPickerViewStyleEducation || _style == LYQEditPositionPickerViewStyleExperience) {
        return _titlesArr[row];
    } else {

        return [NSString stringWithFormat:@"%@K",_salaryArr[component][row]];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (_style == LYQEditPositionPickerViewStyleEducation) {
        _education = _titlesArr[row];
    } else if (_style == LYQEditPositionPickerViewStyleExperience) {
        _experience = _titlesArr[row];
    } else if (_style == LYQEditPositionPickerViewStyleSalary) {
        
        if (component == 0) {
            
            _minSalary = _minArr[row];
            
            if (_minSalary.integerValue >= _maxSalary.integerValue) {
                _maxSalary = _maxArr[row];
                [_pickerView selectRow:row inComponent:1 animated:YES];
            }
            
        } else {
            
            _maxSalary = _maxArr[row];
            
            if (_maxSalary.integerValue <= _minSalary.integerValue) {
                _minSalary = _minArr[row];
                [_pickerView selectRow:row inComponent:0 animated:YES];
            }
        }
    }
}

#pragma mark - model
- (void)setModel:(LYQEditPositionModel *)model {
    _model = model;
}


@end
